package com.collaboration.proton.controllers;


import com.collaboration.proton.dao.GameZoneRepository;
import com.collaboration.proton.dao.HeroRepository;
import com.collaboration.proton.dao.PlayerInformationRepository;
import com.collaboration.proton.dao.SingleAgencyInfoRepository;
import com.collaboration.proton.entities.*;
import com.collaboration.proton.services.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.security.Principal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author vybfi
 * @param
 * @return 无
 * 这个类是save的控制层,用于接受主页的数据输入
 */
@Controller
@RequestMapping("save")
public class SaveController {

    @Autowired
    SingleAgencyInfoService singleAgencyInfoService;

    @Autowired
    HeroService heroService;

    @Autowired
    GameZoneService gameZoneService ;

    @Autowired
    PlayerInformationService playerInformationService;

    @Autowired
    PlayerService playerService;

    @Autowired
    UserHeroService userHeroService;

    @Autowired
    SingleAgencyInfoRepository singleAgencyInfoRepository;



    @GetMapping()
    public String displaySave(Model model){

//        List<Hero> iHero=heroRepository.findAll();
        Hero iHero = new Hero();
        model.addAttribute("H",iHero);

        SingleAgencyInfo singleAgencyInfo =new SingleAgencyInfo();
        model.addAttribute("singleAgencyInfo",singleAgencyInfo);

        GameZone iZone = new GameZone();
        model.addAttribute("Z",iZone);

        PlayerInformation playerInformation = new PlayerInformation();
        model.addAttribute("P", playerInformation);

        return "save/save";
    }

    @PostMapping("saveHero")
    public String saveHero(Model model,Hero hero,Principal principal){

        String name = principal.getName();
        Player player = playerService.findPlayerByUserName(name);
        long Id = player.getUserId();

        if (userHeroService.findUserHeroByPlayer(player) == null) {
            UserHero userHero = new UserHero();
            userHero.setPlayer(player);
            hero.setUserHero(userHero);
        }else {
            UserHero userHero = userHeroService.findUserHeroByPlayer(player);
            hero.setUserHero(userHero);
        }


        heroService.save(hero);

        return "redirect:/save";
    }


    @PostMapping("saveSingleAgencyInfo")
    public String saveSingleAgencyInfo(Model model
            ,SingleAgencyInfo singleAgencyInfo
            ,Principal principal){
        String name = principal.getName();
        Player player = playerService.findPlayerByUserName(name);
        long Id = player.getUserId();

        singleAgencyInfo.setPlayer(player);

        List<SingleAgencyInfo> singleAgencyInfoNow=singleAgencyInfoService.findAllByPlayer(player);

        singleAgencyInfoNow.add(singleAgencyInfo);

        singleAgencyInfoRepository.saveAll(singleAgencyInfoNow);

        return "redirect:/save";
    }

    @PostMapping("saveZone")
    public String createZone(Model model, GameZone gameZone,Principal principal){

        String name = principal.getName();
        Player player = playerService.findPlayerByUserName(name);
        long Id = player.getUserId();

//        if (gameZoneService.findAllByPlayers(player) == null) {
//            GameZone gameZoneNew =new GameZone("请上传数据");
//            gameZoneNew.getPlayers().add(player);
//            gameZoneService.save(gameZoneNew);
//            gameZoneService.findAllByPlayers(player).add(gameZone);
//        }else {}

//        if (gameZone.getPlayers() == null) {
//            gameZone.getPlayers().add(player);
//        }else {
//
//        }
//        List<Player> players=new ArrayList<>();
//        players.add(player);
//        gameZone.setPlayers(players);
//        gameZoneService.findAllByPlayers(player).add(gameZone);
//        gameZone.getPlayers().add(player);
        gameZoneService.save(gameZone);

        return "redirect:/save";

    }

    @PostMapping("savePlayerInformation")
    public String createZone(
            Model model,
            PlayerInformation playerInformation,
            Principal principal) {
        String name = principal.getName();
        Player player = playerService.findPlayerByUserName(name);
        long Id = player.getUserId();


            PlayerInformation playerInformationNow = new PlayerInformation();
            playerInformationNow = playerInformationService.findPlayerInformationByPlayer(player);

            playerInformationNow.setGrade(playerInformation.getGrade());
            playerInformationNow.setWinRate(playerInformation.getWinRate());

            playerInformationService.save(playerInformationNow);


        return "redirect:/save";
    }


}
